#!/bin/bash - 
#
#  Usage:     update vim scripts maintained by github
#  Author:    bucunzai <freecityer@gmail.com>
#  Version:   4.0
#  Created:   03/05/2011 10:01:43 PM CST
#

set -o nounset                              # Treat unset variables as an error

ScriptDIR=$PWD
LinksDIR=$HOME'/.vim/bundle'

function usage {
    cat <<EOF
  Usage: `basename $0` init   , download vim scripts from github
     or: `basename $0` update , update scripts from github using git pull
     or: `basename $0` install, install scripts to \$HOME/.vim/bundle
EOF
}

if [ $# != 1 ]; then
    usage
else
    case "$1" in
        "init" )
            # get scripts list by this command:
            #find . -name config | xargs grep "github.com" | awk '{ print $4 }' | sort -t/ -k 5
            git clone git://github.com/vim-scripts/bash-support.vim.git
            git clone git://github.com/vim-scripts/bufexplorer.zip.git
            git clone git://github.com/vim-scripts/c.vim.git
            git clone git://github.com/Shougo/neocomplcache.git
            git clone git://github.com/vim-scripts/python.vim--Vasiliev.git
            git clone git://github.com/msanders/snipmate.vim.git
            git clone git://github.com/vim-scripts/taglist.vim.git
            git clone git://github.com/tpope/vim-pathogen.git
            git clone git://github.com/tpope/vim-surround.git
            git clone git://github.com/drmingdrmer/xptemplate-dist.git
            git clone git://github.com/mattn/zencoding-vim.git
            echo "Vim scripts ready."
            ;;
        "update" | "up" )
            DirList=`find . -maxdepth 1 -type d | grep -v '\.$' | grep -v 'templates' | sort`
            for adir in $DirList
            do
                cd $ScriptDIR/$adir
                echo -n `basename $PWD`
                echo -n " "
                git pull
            done
            echo "Vim scripts updated."
            ;;
        "install" )
            #DirList=`find . -maxdepth 1 -type d | grep -v '\.$' | grep -v 'templates'`
            #for adir in $DirList
            #do
            #    cd $ScriptDIR/$adir
            #    echo -n `basename $PWD`
            #    echo " installed"
            #    cp -R ./* ~/.vim/
            #done

            # Link all scripts except:
            #      itself | bash-support | c-support | pathogen | templates 
            DirList=`find $ScriptDIR -maxdepth 1 -type d | grep -v 'vimscripts$\|bash-support\|c.vim\|pathogen\|templates' | sort`
            for adir in $DirList
            do
                linkname=`basename $adir`
                #echo $adir
                #echo $LinksDIR
                #echo $linkname
                rm $LinksDIR/$linkname
                ln -s $adir $LinksDIR/$linkname
            done
            # bash-support & c-support
            cd $ScriptDIR/bash-support.vim
            cp -R ./* ~/.vim/
            cd $ScriptDIR/c.vim
            cp -R ./* ~/.vim/
            # templates
            cd $ScriptDIR/templates
            cp -R ./* ~/.vim/
            echo "Vim scripts installed."
            ;;
        * )
            usage
            ;;
    esac
fi
# Back to OLDPWD
cd $ScriptDIR

exit 0
